package curves;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.util.ArrayList;


public class CurveTracerUser extends CurveTracer {
	private static final long serialVersionUID = 1L;
	
	boolean zoomed = false;

	private ArrayList<FunctionVariations> fvar_tab = null;
	private ArrayList<String> fvarName_tab = null;
	private ArrayList<Color> fvarColor_tab = null;
	
	private float thickness = 1.0f;
	
	
	public CurveTracerUser(FunctionVariations var, String var_name) {
		super(var);
		setBackground(Color.white);
		//Ajout d'une fonction dans la liste fvar_tab
		fvar_tab = new ArrayList<FunctionVariations>();
		fvar_tab.add(var);
		
		//Puis ajout du nom
		fvarName_tab = new ArrayList<String>();
		fvarName_tab.add(var_name);
		
		//Et de sa couleur
		fvarColor_tab = new ArrayList<Color>();
		fvarColor_tab.add(Color.blue);

	}
	
	
	public void addFunctionVariations(FunctionVariations var, String var_name, Color c){
		fvar_tab.add(var);
		fvarName_tab.add(var_name);
		fvarColor_tab.add(c);
	}
	
	
	public void removeFunVar(int index){
		fvar_tab.remove(index);
		fvarName_tab.remove(index);
		fvarColor_tab.remove(index);
	}
	
	public void clear(){
		fvar_tab.clear();
		fvarName_tab.clear();
		fvarColor_tab.clear();
	}

	public FunctionVariations getFunctionVariations(int index){
		return fvar_tab.get(index);
	}
	
	public String[] getFunVarNames(){
		String[] funVarNames = new String[fvarName_tab.size()];
		int i = 0;
		for(String fvar : fvarName_tab)
			funVarNames[i++] = fvar;
		return funVarNames;
	}
	
	public void setThickness(float thickness){
		this.thickness = thickness;
	}
	
	public void setZoom(boolean bool){
		zoomed = bool;
	}
	
	public boolean getZoom(){
		return zoomed;
	}

	
	public void tabulateThemAll(int step){
	    for(FunctionVariations fvar : fvar_tab)
	    	fvar.tabulate(step);
	}
	
	 
	public void setXmin(double xmin){
		for(FunctionVariations fvar : fvar_tab)
			fvar.setXmin(xmin);
	}
	
	public void setXmax(double xmax){
		for(FunctionVariations fvar : fvar_tab)
			fvar.setXmax(xmax);
	}
	
	public double getXmin(){
		if(fvar_tab.size() == 0)
			return 0;
		else{
			double xmin = fvar_tab.get(0).getXmin();
			for(FunctionVariations fvar : fvar_tab){
				if (fvar.getXmin() < xmin);
					xmin = fvar.getXmin();
			}
			return xmin;
		}
	}
	
	public double getXmax(){
		if(fvar_tab.size() == 0)
			return 0;
		else{
			double xmax = fvar_tab.get(0).getXmax();
			for(FunctionVariations fvar : fvar_tab){
				if (fvar.getXmax() < xmax);
					xmax = fvar.getXmax();
			}
			return xmax;
		}
	}
	
	public double getYmin(){
		if(fvar_tab.size() == 0)
			return 0;
		else{
			double ymin = fvar_tab.get(0).getYmin();
			for(FunctionVariations fvar : fvar_tab){
				if (fvar.getYmin() < ymin);
					ymin = fvar.getYmin();
			}
			return ymin;
		}
	}
	
	public double getYmax(){
		if(fvar_tab.size() == 0)
			return 0;
		else{
			double ymax = fvar_tab.get(0).getYmax();
			for(FunctionVariations fvar : fvar_tab){
				if (fvar.getYmax() < ymax);
					ymax = fvar.getYmax();
			}
			return ymax;
		}
	}
	

	
	


	
	protected void paintComponent(Graphics g) {
		
		//Epaisseur de la ligne
		BasicStroke stroke = new BasicStroke(thickness);    
		Graphics2D g2 = (Graphics2D)g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setStroke( stroke );

		int width = getWidth();

		// Paint background if we're opaque.
		if (isOpaque()) {
			g.setColor(getBackground());
			g.fillRect(0, 0, width, getHeight());
		}

			
		for(int f_i=0; f_i < fvar_tab.size(); f_i++){
			double step = ((double) width) / fvar_tab.get(f_i).getStepNumber();
			double lastX = 0;
			double newX;

			int lastY = getY(fvar_tab.get(f_i).getStepValue(0));
			int newy;
			for (int i = 0; i < fvar_tab.get(f_i).getStepNumber(); i++) {
				newy = getY(fvar_tab.get(f_i).getStepValue(i + 1));
				newX = lastX + step;
				
				g.setColor(fvarColor_tab.get(f_i));
				g.drawLine((int) lastX, lastY, (int) newX, newy);
				lastY = newy;
				lastX = newX;
			}
			if (lastX < (double) width) {
				g.drawLine((int) lastX, lastY, width, lastY);
			}
		
			}
	}
}
